package service;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.*;
import domain.CD;

public class SimpleCDManager implements CDManager {
  private List<CD> elements;
  private List<CD> CDs_tmp;

  public List<CD> getAll() {
    return elements;
  }

  public void setCDs(List<CD> elements) {this.elements = elements;}
  public List<CD> getCDs(){return this.elements;}

  public void setCDs_tmp(List<CD> CDs_tmp){this.CDs_tmp=CDs_tmp;}
  public List<CD> getCDs_tmp(){return this.CDs_tmp;}
  
  public CD getById(String id) {
    Iterator itr = elements.iterator();
    CD element;
    while (itr.hasNext()) {
      element = (CD)itr.next();
      if (element.getPk_CD().equals(id)) return element;
    }
    return null;
  }

  public List<CD> getAllByBandaID(String id){
    CDs_tmp.clear();
    Iterator itr = elements.iterator();
    CD element;
    while (itr.hasNext()) {
      element = (CD)itr.next();
      if (element.getFk_banda().equals(id))
        CDs_tmp.add(element);      
    }
    return CDs_tmp;
  }
  
  public void delete(String id){

    Iterator itr = elements.iterator();

    while (itr.hasNext()) {
      CD element;
      element = (CD)itr.next();
      if (element.getPk_CD  ().equals(id)){
        itr.remove();
  	  }
    }
  }
  
  public void insert(CD myObj){

    if (myObj.getNombre()!=null){
      Random generator = new Random();
      int id = generator.nextInt();
	    id = Math.abs(id);
		  myObj.setPk_CD(String.valueOf(id));
		  elements.add(myObj);
    }
  }
}